package com.noocodecommit.wicket.jquery.plugin.ui.selectable;

import java.util.ArrayList;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;

import com.noocodecommit.wicket.jquery.TemplatePage;
import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.plugin.chili.JQChili;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.selectable.JQSelectable;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.selectable.JQSelectableUtils;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.selectable.OnSelectStartBehavior;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.selectable.OnSelectStopBehavior;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.selectable.OnSelectingBehavior;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.selectable.OnUnselectingBehavior;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.tabs.JQTabs;

public class SelectablePage extends TemplatePage
{

	private static final long serialVersionUID = 1L;
	private final JQTabs tabContainer;

	public SelectablePage()
	{
		super();
		JQCore.init(this);
		this.setupChili();

		tabContainer = new JQTabs("tab-container");
		tabContainer.setOutputMarkupId(true);
		add(tabContainer);

		/***********************
		 * Simple Selectable
		 ***********************/
		JQSelectable selectable0 = JQSelectableUtils.create("selectable0").build();
		tabContainer.add(selectable0);

		/***********************
		 * Ajax Selectable #1
		 ***********************/
		JQSelectable selectable1 = JQSelectableUtils.create("selectable1").build();

		ArrayList<String> list1 = new ArrayList<String>();
		list1.add("Cat");
		list1.add("Dog");
		list1.add("Bird");
		list1.add("Crocodile");
		list1.add("Murloc");
		selectable1.add(new ListView("item1", list1)
		{
			@Override
			protected void populateItem(ListItem item)
			{
				item.add(new Label("content1", item.getModel()));
				item.setOutputMarkupId(true);
			}
		});
		tabContainer.add(selectable1);

		final Label output1 = new Label("output1");
		output1.setOutputMarkupId(true);
		tabContainer.add(output1);

		selectable1.add(new OnSelectStartBehavior()
		{
			public void onStart(AjaxRequestTarget target)
			{
				String msg = String.format("selection start");
				output1.setModel(new Model(msg));
				target.addComponent(output1);
			}
		});

		selectable1.add(new OnSelectStopBehavior()
		{
			public void onStop(AjaxRequestTarget target)
			{
				String msg = String.format("selection stop");
				output1.setModel(new Model(msg));
				target.addComponent(output1);
			}
		});

		selectable1.add(new OnSelectingBehavior()
		{
			public void onSelecting(AjaxRequestTarget target)
			{
				String msg = String.format("selecting item '%s'", ((JQSelectable) this.getComponent()).getSelecting().getModelObjectAsString());
				output1.setModel(new Model(msg));
				target.addComponent(output1);
			}
		});

		selectable1.add(new OnUnselectingBehavior()
		{
			public void onUnselecting(AjaxRequestTarget target)
			{
				String msg = String.format("selecting item '%s'", ((JQSelectable) this.getComponent()).getUnselecting().getModelObjectAsString());
				output1.setModel(new Model(msg));
				target.addComponent(output1);
			}
		});
		
		/***********************
		 * Ajax Selectable #2
		 ***********************/
		JQSelectable selectable2 = JQSelectableUtils.create("selectable2").build();

		ArrayList<String> list2 = new ArrayList<String>();
		list2.add("Cat");
		list2.add("Dog");
		list2.add("Bird");
		list2.add("Crocodile");
		list2.add("Murloc");

		selectable2.add(new ListView("item2", list2)
		{
			protected void populateItem(ListItem item)
			{
				item.add(new Label("content2", item.getModel()));
				item.setOutputMarkupId(true);
			}
		});
		selectable2.setOutputMarkupId(true);
		tabContainer.add(selectable2);

		final Label output2 = new Label("output2");
		output2.setOutputMarkupId(true);
		tabContainer.add(output2);

		selectable2.add(new OnSelectStopBehavior()
		{
			public void onStop(AjaxRequestTarget target)
			{
				String selectedItems = "";
				for (int i = 0; i < ((JQSelectable) this.getComponent()).getSelectedComponents().size(); i++)
				{
					selectedItems = selectedItems + ((JQSelectable) this.getComponent()).getSelectedComponents().get(i).getModelObjectAsString() + ", ";
				}
				selectedItems = selectedItems.substring(0, selectedItems.lastIndexOf(","));
				String msg = String.format("selected items '%s'", selectedItems);
				output2.setModel(new Model(msg));
				target.addComponent(output2);
			}

		});
		
		JQChili chili1 = new JQChili("chili1");
		add(chili1);
		this.bindChiliCode(chili1, this.getClass(), "html");

		JQChili chili2 = new JQChili("chili2");
		add(chili2);
		this.bindChiliCode(chili2, this.getClass(), "java");

		JQCore.get().bindOnDocumentReadyFunction();
	}
}
